package com.moxiao.yfjh.module.blind.service.album;

import com.moxiao.yfjh.module.blind.controller.admin.album.vo.BlindMemberAlbumCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.album.vo.BlindMemberAlbumExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.album.vo.BlindMemberAlbumPageReqVO;
import com.moxiao.yfjh.module.blind.convert.album.BlindMemberAlbumConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.album.BlindMemberAlbumDO;
import com.moxiao.yfjh.module.blind.dal.mysql.album.BlindMemberAlbumMapper;
import com.moxiao.yfjh.module.system.sms.utils.GreenScanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import com.moxiao.yfjh.module.blind.controller.admin.album.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员相册 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class BlindMemberAlbumServiceImpl implements BlindMemberAlbumService {

    @Resource
    private BlindMemberAlbumMapper memberAlbumMapper;

    @Override
    public void createMemberAlbum(List<BlindMemberAlbumCreateReqVO> createReqVO, Long userId) {
        // 插入
        List<BlindMemberAlbumDO> blindMemberAlbumDOS = BlindMemberAlbumConvert.INSTANCE.convert(createReqVO);
        blindMemberAlbumDOS.forEach(blindMemberAlbumDO -> {
            blindMemberAlbumDO.setUserId(userId);
        });

        Collection<BlindMemberAlbumDO> memberAlbumDOS = new ArrayList<>(blindMemberAlbumDOS);
        memberAlbumMapper.insertBatch(memberAlbumDOS);
    }

    @Override
    public void updateMemberAlbum(List<BlindMemberAlbumCreateReqVO> createReqVO,Long userId) {
        BlindMemberAlbumExportReqVO memberAlbumReqVO = new BlindMemberAlbumExportReqVO();
        memberAlbumReqVO.setUserId(userId);
        List<BlindMemberAlbumDO> memberAlbumList = getMemberAlbumList(memberAlbumReqVO);
        if (CollectionUtils.isNotEmpty(memberAlbumList)){
            List<Long> collect = memberAlbumList.stream().map(BlindMemberAlbumDO::getId).collect(Collectors.toList());
            memberAlbumMapper.deleteBatchIds(collect);
        }

        List<BlindMemberAlbumDO> blindMemberAlbumDOS = BlindMemberAlbumConvert.INSTANCE.convert(createReqVO);
        blindMemberAlbumDOS.forEach(blindMemberAlbumDO -> {
            blindMemberAlbumDO.setUserId(userId);
        });
        Collection<BlindMemberAlbumDO> memberAlbumDOS = new ArrayList<>(blindMemberAlbumDOS);
        memberAlbumMapper.insertBatch(memberAlbumDOS);
    }


    @Override
    public void deleteMemberAlbum(Long id) {
        // 校验存在
        validateMemberAlbumExists(id);
        // 删除
        memberAlbumMapper.deleteById(id);
    }

    private void validateMemberAlbumExists(Long id) {
        if (memberAlbumMapper.selectById(id) == null) {
            throw exception(MEMBER_ALBUM_NOT_EXISTS);
        }
    }

    @Override
    public BlindMemberAlbumDO getMemberAlbum(Long id) {
        return memberAlbumMapper.selectById(id);
    }

    @Override
    public List<BlindMemberAlbumDO> getMemberAlbumList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberAlbumMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlindMemberAlbumDO> getMemberAlbumPage(BlindMemberAlbumPageReqVO pageReqVO) {
        return memberAlbumMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlindMemberAlbumDO> getMemberAlbumList(BlindMemberAlbumExportReqVO exportReqVO) {
        return memberAlbumMapper.selectList(exportReqVO);
    }

    @Override
    public Long getBlindMemberAlbumCount(Long userId) {
        return memberAlbumMapper.selectCount(BlindMemberAlbumDO::getUserId,userId);
    }

}
